#include "../ModelEditorConfig.h"
#include "TextureViewDialog.h"



TextureViewComponent::TextureViewComponent( const TCHAR* texName )
{
	m_TextureName = texName;
	m_ImageScale = 1.0f;
	m_RenderScale = 1.0f;

	UINT index = 0;

	UINT tex_width, tex_height;
	char* pTexData = NULL;

	setOpaque(true);

	if( !g_pDevice->GetSkinManager()->GetTextureInfo(texName, tex_width, tex_height, m_colorbits) )
	{
		AlertWindow::showMessageBox(AlertWindow::WarningIcon, "Can't open file ", String(texName));
	}
	else
	{
		UINT numBytesOfData = tex_width*tex_height*m_colorbits/8;
		pTexData = new char[numBytesOfData];
		if( g_pDevice->GetSkinManager()->LoadTextureData(texName, (void*)pTexData) )
		{
			if( m_colorbits == 32 )
			{
				Image myImage(Image::ARGB, tex_width, tex_height, true);
				for(UINT y = 0; y < tex_height; y++) 
				{
					for(UINT x = 0; x < tex_width; x++)
					{
						uint8 blue	= ((BYTE*)pTexData)[index];
						uint8 green	= ((BYTE*)pTexData)[index+1];
						uint8 red	= ((BYTE*)pTexData)[index+2];
						uint8 alpha	= ((BYTE*)pTexData)[index+3];
						myImage.setPixelAt(x, y, Colour(red, green, blue, alpha));
						index += 4;
					}
				}
				argbImage = myImage.createCopy();
			}
			else if( m_colorbits == 24 )
			{
				Image myImage(Image::RGB, tex_width, tex_height, true);
				for(UINT y = 0; y < tex_height; y++) 
				{
					for(UINT x = 0; x < tex_width; x++)
					{
						uint8 blue	= ((unsigned char*)pTexData)[index];
						uint8 green = ((unsigned char*)pTexData)[index+1];
						uint8 red	= ((unsigned char*)pTexData)[index+2];

						myImage.setPixelAt(x, y, Colour(red, green, blue));
						index += 3;
					}
				}
				rgbImage = myImage.createCopy();
			}
		}
	}

	if( pTexData )
	{
		delete [] pTexData;
		pTexData = NULL;
	}


	m_ImageWidth = tex_width;
	m_ImageHeight = tex_height;

	while( (m_ImageWidth*m_ImageScale > 512) || (m_ImageHeight*m_ImageScale > 512) )
	{
		m_ImageScale *= 0.5f;
	}

	addAndMakeVisible(m_ScaleComboBox = new ComboBox(L"Texture Zoom"));
	m_ScaleComboBox->setTooltip(L"Choose texture zoom in/out");
    m_ScaleComboBox->setEditableText(false);
    m_ScaleComboBox->setJustificationType(Justification::centredLeft);
	m_ScaleComboBox->addListener(this);
	m_ScaleComboBox->addItem(L"25%", 1);
    m_ScaleComboBox->addItem(L"50%", 2);
    m_ScaleComboBox->addItem(L"75%", 3);
    m_ScaleComboBox->addItem(L"100%", 4);
	m_ScaleComboBox->setSelectedId(static_cast<int>(GetImageScale()/0.25f));
	for(int i=1; i<=4; ++i)
	{
		if( i > m_ScaleComboBox->getSelectedId() )
			m_ScaleComboBox->setItemEnabled(i, false);
	}

	addAndMakeVisible(m_ChannelComboBox = new ComboBox(L"Channel"));
	m_ChannelComboBox->setTooltip(L"Choose view channel of texture");
    m_ChannelComboBox->setEditableText(false);
    m_ChannelComboBox->setJustificationType(Justification::centredLeft);
	m_ChannelComboBox->addListener(this);
	m_ChannelComboBox->addItem(L"Alpha Channel Only", 1);
    m_ChannelComboBox->addItem(L"RGB Channel Only", 2);
    m_ChannelComboBox->addItem(L"RGBA Channel", 3);
	if( m_colorbits == 24 )
	{
		m_ChannelComboBox->setSelectedId(2);
		m_ChannelComboBox->setItemEnabled(1, false);
		m_ChannelComboBox->setItemEnabled(3, false);
	}
	else if( m_colorbits == 32 )
	{
		m_ChannelComboBox->setSelectedId(3);
	}

	addAndMakeVisible(m_ScaleLabel = new Label(L"Texture Zoom",L"Texture Zoom"));
	m_ScaleLabel->setEditable(false, false, false);

	addAndMakeVisible(m_ChannelLabel = new Label(L"Channel",L"Channel"));
	m_ChannelLabel->setEditable(false, false, false);

	setBounds(0, 0, static_cast<UINT>(m_ImageWidth*m_ImageScale), static_cast<UINT>(m_ImageHeight*m_ImageScale+60));
}

void TextureViewComponent::paint(Graphics& g)
{
	g.fillAll(findColour(AlertWindow::backgroundColourId));
    g.setImageResamplingQuality(Graphics::highResamplingQuality);

	juce::Rectangle<int> ImageLocalBounds(0, 0, static_cast<int>(m_ImageWidth*m_ImageScale), static_cast<int>(m_ImageHeight*m_ImageScale));
    g.fillCheckerBoard(ImageLocalBounds, 50, 50,
		Colour(0xffdddddd), Colours::transparentBlack);

	if( m_colorbits == 32 )
	{		
		AffineTransform transform( AffineTransform::scale(m_ImageScale*m_RenderScale, m_ImageScale*m_RenderScale) );
        if( m_ChannelComboBox->getSelectedId() == 1 )
			g.drawImageTransformed(alphaImage, transform, false);
		else if( m_ChannelComboBox->getSelectedId() == 2 )
			g.drawImageTransformed(rgbImage, transform, false);
		else
			g.drawImageTransformed(argbImage, transform, false);
	}
	else if( m_colorbits == 24 )
	{
		AffineTransform transform( AffineTransform::scale(m_ImageScale*m_RenderScale, m_ImageScale*m_RenderScale) );
        g.drawImageTransformed(rgbImage, transform, false);
	}
}

void TextureViewComponent::resized()
{
	m_ScaleLabel->setBounds(0, static_cast<UINT>(m_ImageHeight*m_ImageScale)+6, 96, 24);
	m_ScaleComboBox->setBounds(100, static_cast<UINT>(m_ImageHeight*m_ImageScale)+6, 150, 24 );

	m_ChannelLabel->setBounds(0, static_cast<UINT>(m_ImageHeight*m_ImageScale)+30, 96, 24);
	m_ChannelComboBox->setBounds(100, static_cast<UINT>(m_ImageHeight*m_ImageScale)+30, 150, 24 );
}

void TextureViewComponent::comboBoxChanged(ComboBox* comboBoxThatHasChanged)
{
	if( comboBoxThatHasChanged == m_ScaleComboBox )
	{
		int selectID = m_ScaleComboBox->getSelectedId();
		if( m_ImageScale > selectID * 0.25f )
			m_RenderScale = selectID * 0.25f / m_ImageScale;
		else 
			m_RenderScale = 1.0f;

	}
	else if( comboBoxThatHasChanged == m_ChannelComboBox )
	{
		if( m_colorbits == 32 )
		{
			if( m_ChannelComboBox->getSelectedId() == 1 )
			{
				rgbImage = argbImage.convertedToFormat(Image::SingleChannel);
				alphaImage = rgbImage.convertedToFormat(Image::RGB);
			}
			else if( m_ChannelComboBox->getSelectedId() == 2 )
			{
				rgbImage = argbImage.convertedToFormat(Image::RGB);
			}
		}
	}

	repaint();
}
